package org.medicalinstrument.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.medicalinstrument.bean.Customer;
import org.medicalinstrument.bean.CustomerAddress;
import org.medicalinstrument.bean.PurchaseOrder;
import org.medicalinstrument.dao.CustomerDao;
import org.medicalinstrument.database.DataSourceUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CustomerDaoImpl
 * @Description TODO
 * @AUTHOR cirly
 * @DaTe 2018/11/3 20:40
 * @Modify by
 * @Version 1.0
 **/
public class CustomerDaoImpl implements CustomerDao {
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    /**
     * @desc ： login in
     * @author : cirly
     * @param : accountNo  ,  password
     * @return : Customer
    **/

    @Override
    public Customer customerLogin(String custNo, String pass) throws SQLException {
        String sql="select * from customeraccount where accountNo=? and password=?";
        Customer customer = qr.query(sql,new BeanHandler<>(Customer.class),custNo,pass);
        System.out.println(customer);
        return customer;
    }
    /**
     * @desc ： judge if a customer exisit
     * @author : cirly
     * @param : accountNo
     * @return : Customer
    **/

    @Override
    public Customer isExist(String custNo) throws SQLException {
        String sql="select * from customeraccount where accountNo=?";
        Customer customer = qr.query(sql,new BeanHandler<>(Customer.class),custNo);
        System.out.println(customer);
        return customer;
    }
    /**
     * @desc ：register a new customer
     * @author : cirly
     * @param :
     * @return :  success or error
    **/

    @Override
    public String register(String custPhone, String custName, String gender, String custAddr, int custAge, String connect, String pwd, String custNo) throws SQLException{
        String sql = "insert into  customeraccount values (?,?,?,?,?,?,?,?)";
        //System.out.println(sql);
        qr.update(sql,custName,custPhone,pwd,custAddr,gender,custAge,connect,custNo);
        return "success";
    }

    /**
     * @desc ：get a customer's address
     * @author : cirly
     * @param : accountNo
     * @return : CustomerAddrss
    **/
    @Override
    public CustomerAddress getAddress(String accountNo) throws SQLException {

        String sql = "select * from customeraddress where accountNo = ?";
        CustomerAddress customerAddress = qr.query(sql,new BeanHandler<>(CustomerAddress.class),accountNo);
        return customerAddress;
    }
    /**
     * @desc ：increase receipt address
     * @author : cirly
     * @param :  CustomerAddress
     * @return :  int
    **/
    @Override
    public int addAddress (CustomerAddress address) throws SQLException{
        int count = 0;
        System.out.println("imp :  "+address);
        String sql = "insert into customeraddress (accountNo,custName,custPhone,province,city,region,detailedAddress,tagName,code) values (?,?,?,?,?,?,?,?,?)";
        //String sql = "insert into customeraddress  values (?,?,?,?,?,?,?,?,?)";
        count = qr.update(sql,address.getAccountNo(),address.getCustName(),address.getCustPhone(),address.getProvince(),address.getCity(),address.getRegion(),address.getDetailedAddress(),
                address.getTagName(),address.getCode());
        return count;
    }

    @Override
    public List<Customer> getAllCustomerList() throws SQLException {
        System.out.println("impl");
        String sql = "select * from customeraccount";
        List<Customer> list = qr.query(sql,new BeanListHandler<>(Customer.class));
        System.out.println(list);
        return list;
    }

    @Override
    public List<Customer> Search(String accountNo, String custPhone) throws SQLException {
        String sql = "select * from  customeraccount ";
        System.out.println(sql);
        List list =  new ArrayList();
        if(!"".equals(accountNo)){
            String sqla = "accountNo='" + accountNo +"'";
            list.add(sqla);
        }
        if(!"".equals(custPhone)){
            String sqla = "custPhone='" + custPhone +"'";
            list.add(sqla);
        }
        if(list.size()>0){
            sql += " where 1=1 and " + String.join(" and ",list);
        }
        System.out.println(sql);
        List<Customer> list1 = qr.query(sql,new BeanListHandler<>(Customer.class));
        System.out.println(list1);
        return list1;
    }

    @Override
    public int modifycustomer(String custPhone, String accountNo, String password, String consigeeAddr, String custName, String sex, String age) {
        return 0;
    }

    @Override
    public Boolean delete(String accountNo) throws SQLException {
        String sql = "delete from customeraccount where accountNo=?";
        int update = qr.update(sql, accountNo);
        return update>0;
    }

    @Override
    public Boolean deleteOrder(String orderId, String accountNo, String productQR) throws SQLException {
        System.out.println(orderId+" "+accountNo+" "+productQR);
        int update = 0 ;
        String sql = "delete from  purchaseorderdetail where orderId = "+ orderId +
                " and accountNo = "+accountNo+ " and productQR = "+productQR;

        //String sql = "insert into customeraddress  values (?,?,?,?,?,?,?,?,?)";
        System.out.println(sql);
       update = qr.update(sql);
        return update > 0;
    }

    /**
     * @desc ：update a customerAddress
     * @author : cirly
     * @param :  CustomerAddress
     * @return : int
    **/
    @Override
    public int updateAddress(CustomerAddress address) throws SQLException{
        int count = 0;
        String sql = "update customeraddress set custName = ? ,custphone = ? , province = ? , city = ?," +
                "region = ? , detailedAddress = ?, tagName = ?, code = ? where  id = ? and accountNo = ?";
        //System.out.println(cart);
        int update = qr.update(sql, address.getCustName(),address.getCustPhone(),address.getProvince(),address.getCity(),address.getRegion(),
                address.getDetailedAddress(),address.getTagName(),address.getCode(),address.getId(),address.getAccountNo());
        return count;
    }

}
